package com.door.erp.sevice.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.door.erp.dao.OrderDao;
import com.door.erp.dao.base.GenericDao;
import com.door.erp.enums.OrderStatus;
import com.door.erp.model.Orders;
import com.door.erp.pager.Pager;
import com.door.erp.sevice.OrderService;
import com.door.erp.sevice.base.GenericServiceImpl;
import com.mysql.jdbc.StringUtils;

@Service(value="orderService")
public class OrderServiceImp extends GenericServiceImpl<Orders, Long> implements
		OrderService {
	
	
	@Resource(name="orderDao",type=OrderDao.class)
	private OrderDao ordersDao;

	@Override
	public GenericDao<Orders, Long> getDao() {
		// TODO Auto-generated method stub
		return ordersDao;
	}
	
	public List<Orders> listOrderByPage(Pager pager){
		return this.ordersDao.listOrders( pager.getStartRow(), pager.getNumPerPage());
	}
	
	public int listOrdersCount(){
		return this.ordersDao.listOrdersCount();
	}

	@Override
	public List<Orders> listOrderByPage(Map<String, Object> params, Pager pager) {
		return this.ordersDao.listOrders(params,  pager.getStartRow(), pager.getNumPerPage());
	}

	@Override
	public int listOrdersCount(Map<String, Object> params) {
		// TODO Auto-generated method stub
		return this.ordersDao.listOrdersCount(params);
	}

	public List<Orders> getOrdersByExport(String beginTimeStr, String endTimeStr, String orderStatusStr) {
		SimpleDateFormat  format = new SimpleDateFormat("yyyy-MM-dd");
		if(StringUtils.isNullOrEmpty(beginTimeStr) || StringUtils.isNullOrEmpty(endTimeStr)) {
			return null; 
		}
		int status = Integer.parseInt(orderStatusStr);
		OrderStatus orderStatus = null;
		if(status == OrderStatus.SAVE.ordinal()) {
			orderStatus = OrderStatus.SAVE;
		} else if(status == OrderStatus.WAIT_VERIFY.ordinal()) {
			orderStatus = OrderStatus.WAIT_VERIFY;
		} else if(status == OrderStatus.PASS_VERIFY.ordinal()) {
			orderStatus = OrderStatus.PASS_VERIFY;
		} else {
			orderStatus = OrderStatus.INVALID;
		}
		Date beginTime = null;
		Date endTime = null;
		try {
			beginTime = format.parse(beginTimeStr);
			endTime = format.parse(endTimeStr);
			endTime = new Date(endTime.getTime() + 24 * 60 * 60 * 1000);
		} catch (ParseException e1) {
			e1.printStackTrace();
			return null;
		}
		return this.ordersDao.getOrdersByExport(beginTime, endTime, orderStatus);
	}
	
}
